<?php
	require_once('../params.inc');
	require_once('../data.inc');
	require_once('../utils.inc');
	require_once('sf.inc');
	require_once('../shop.inc');
	
		//from now on it will be 'productId' param, not keys that is passed
	$id = false;
	if ($par->exist('keys')) {
		$keyFields = new DecodedFieldValues($par->get('keys'));
		$id = $keyFields->getValueOf('cf_products.id');
	}
	elseif ($par->exist('lastInsertId')) {
		$id = $par->get('lastInsertId');
	}
	
	assert($id);
	
	$thisCat = new Category('products');	
	$product = new Product($id);
	
	$path = array(
		array('Главная','index.php'),
		array($thisCat->descr(),'view.php?name=products'),
		"Товар '{$product->name()}' в группах"
	);
	
	require_once('header.inc'); 
?>

<script lang=javascript>
	function submit() 
	{
		var oF = document.getElementById('oForm');
		if (oF.onsubmit()) {
			oF.submit();
		}
	}
</script>	

<form id=oForm action='do_product.php' onsubmit="return true;" method=post>
	<input type=hidden name=do value="groups" />
	<input type=hidden name=productId value='<?php echo $id;?>'/>
	<select name='groups[]' multiple size=20 style="width:100%;">
		<?php
			$nowInGroups = new ProductGroups($id, true);
			
			function printChildren($grId)
			{
				$children = new ChildGroups($grId, true);
				foreach ($children as $child) 
				{
					$ancestors = new AncestorGroups($child->id());
					$path = '';
					foreach ($ancestors as $ancestor) {
						$path .= $ancestor->name() . '/';
					}
					echo "<option value='{$child->id()}'";
					global $nowInGroups;
					if ($nowInGroups->exist($child->id())) {
						echo " selected ";
					}
					echo ">$path{$child->name()}";
					printChildren($child->id());
				}
			}			
			
			printChildren(Group::rootId);
		?>
	</select>
</form>

<table width=100%>
	<tr>
		<td width=100% align=right>
			<button onclick="submit()">Сохранить</button>
		</td>
	</tr>
</table>



<?php require_once('footer.inc'); ?>